package net.wantongtech.xa.uploadbasicinfo.dao;

import com.wtkj.dao.impl.BaseDataTemplate;
import com.wtkj.vo.dzzf.base.kfwd.Account;
import com.wtkj.vo.dzzf.base.qxsjz.entity.DisputeTransOut;
import com.wtkj.vo.dzzf.base.qxsjz.entity.PortalTransOut;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author MaShibo
 * 2021/2/9 0009
 */
@Repository
public class RecoverDisputeTransDao extends BaseDataTemplate {


    public List<DisputeTransOut> queryAllDisputeTarnsOut() {
        final String sql =
                "select tr.*\n" +
                        "  from tr_dispute_transout tr\n" +
                        "  join tr_portal_transout tp\n" +
                        "    on tr.transactionid = tp.transactionid\n" +
                        "  join tr_integrated_transout n\n" +
                        "    on tr.transactionid = n.integratedid\n" +
                        " where tr.processtime > trunc(sysdate)\n" +
                        "   and tp.servicetype = 2\n" +
                        "   and tp.etccardtype = 22\n" +
                        "   and tp.transtime < date '2021-02-01'";
        return jdbcTemplate.query(sql, BeanPropertyRowMapper.newInstance(DisputeTransOut.class));
    }

    public int recoverDisputeTransOut(String transactionId) {
        final String sql =
                " update tr_dispute_transout ds\n" +
                        "    set ds.dealby      = null,\n" +
                        "        ds.dealon      = null,\n" +
                        "        ds.processtime = null,\n" +
                        "        ds.dealflag    = 99,\n" +
                        "        ds.sendflag    = 0,\n" +
                        "        ds.filepath    = 0,\n" +
                        "        ds.reason      = null,\n" +
                        "        ds.result      = 99,\n" +
                        "        ds.disputedid  = null\n" +
                        "  where ds.transactionid = ?";
        return jdbcTemplate.update(sql, transactionId);
    }


    public PortalTransOut queryPortalTransOutById(String transactionId) {
        final String sql = "select tr.accountid,tr.fee from tr_portal_transout tr where tr.transactionid = ?";
        List<PortalTransOut> portalTransOutList = jdbcTemplate.query(sql, BeanPropertyRowMapper.newInstance(PortalTransOut.class), transactionId);
        return portalTransOutList.isEmpty() ? null : portalTransOutList.get(0);
    }


    public Account queryAccountById(String accountId) {
        Account queryObj = new Account();
        queryObj.setAccountId(accountId);
        return (Account) queryObject(queryObj);
    }

    public int deleteAccountConsume(String accountId, String transactionId) {
        final String sql =
                "delete from ac_accountconsume a\n" +
                        " where a.accountid = ?\n" +
                        "   and a.consumeid = ?";
        return jdbcTemplate.update(sql, accountId, transactionId);
    }


    public int recoverPortalTransOut(String transactionId) {
        final String sql =
                "update tr_portal_transout tr\n" +
                "   set tr.state = 10, tr.integratedid = null, tr.isintegrated = 0\n" +
                " where tr.transactionid = ?";
        return jdbcTemplate.update(sql, transactionId);
    }


    public int deleteIntegratedTransOut(String transactionId) {
        final String sql = " delete tr_integrated_transout tr where tr.integratedid = ?";
        return jdbcTemplate.update(sql, transactionId);
    }

}
